package com.ruoyi.web.controller.system;

import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BiOperLogEnum;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.Discount;
import com.ruoyi.system.domain.OperLog;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.UserTb;
import com.ruoyi.system.service.IDiscountService;
import com.ruoyi.system.service.UserTbService;
import com.ruoyi.system.service.impl.DiscountServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏用户控制器
 *
 */
@Controller
@RequestMapping("/gameUser")
public class UserTbController extends BaseController{
    @Autowired
    private UserTbService userTbService;
    @Autowired
    private IDiscountService discountService;

    /**
     * 充值
     * @return
     */
    @RequestMapping("/recharge")
    public String recharge(){
        return "/pages/recharge/recharge";
    }


    /**
     * 基本信息
     * @return
     */
    @RequestMapping("/mation")
    public String mation(){
        return "/pages/user/mation";
    }


    /**
     * 冻结账号页
     * @return
     */
    @RequestMapping("/freeze")
    public String freeze(){
        return "/pages/manage/freeze";
    }

    /**
     * 开通或者解除代理
     * @return
     */
    @RequestMapping("/relieve")
    public String list(){
        return "/pages/manage/relieve";
    }

    @RequiresPermissions("system:user:account")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(String key, Integer pageNum, Integer pageSize)
    {
        List<UserTb> allGameUser = userTbService.getAllGameUser(key, (pageNum-1)*pageSize, pageSize);
        Long total = userTbService.getAllGameUserCount(key,(pageNum-1)*pageSize,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("list",allGameUser);
        map.put("total",total);

        return AjaxResult.success("操作成功",map);
    }

    @RequiresPermissions("system:user:account")
    @PostMapping("/bindAccount")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult bindAccount(Integer status, Integer id)
    {
        //判断参数
        if (status == null || id == null){
            return AjaxResult.error("参数校验失败");
        }
        //先查询该账号是否存在
        UserTb userTb = userTbService.selectGameUserById(id);
        if (userTb==null){
            return AjaxResult.error("该角色不存在");
        }
        OperLog operLog = new OperLog();
        operLog.setCreateBy(ShiroUtils.getLoginName());
        operLog.setType(status==0? BiOperLogEnum.DJ_GAME_USER.getCode():BiOperLogEnum.JD_GAME_USER.getCode());
        operLog.setGameUserId(id.longValue());
        operLog.setCreateTime(new Date());
        return toAjax(userTbService.updateGameUserStatus(status,id,operLog));
    }

    @RequiresPermissions("system:user:proxy")
    @PostMapping("/proxy")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult proxy(Integer isAgent, Integer id)
    {
        //判断参数
        if (isAgent == null || id == null){
            return AjaxResult.error("参数校验失败");
        }
        //先查询该账号是否存在
        UserTb userTb = userTbService.selectGameUserById(id);
        if (userTb==null){
            return AjaxResult.error("该角色不存在");
        }
        OperLog operLog = new OperLog();
        operLog.setCreateBy(ShiroUtils.getLoginName());
        operLog.setType(isAgent==0? BiOperLogEnum.DEL_PROXY.getCode():BiOperLogEnum.INSERT_PROXY.getCode());
        operLog.setGameUserId(id.longValue());
        operLog.setCreateTime(new Date());
        return toAjax(userTbService.updateGameUserAgent(isAgent,id,operLog));
    }

    @RequiresPermissions("system:user:proxyInfo")
    @PostMapping("/proxyInfo")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult proxyInfo()
    {
        Map<String,Object> retMap = new HashMap<>();
        SysUser sysUser = getSysUser();
        //查询当前折扣
        if (sysUser.getRoles().get(0).getRoleId() == 3){
            //查询折扣
            Discount discount = discountService.getDiscountByGameId(sysUser.getGameUserId());
            if (discount != null){
                sysUser.setDiscount(discount.getPayDiscount()==null?new BigDecimal("0.8"):discount.getPayDiscount());
            } else {
                sysUser.setDiscount(new BigDecimal("0.8"));
            }
            //查询当前剩余房卡和金币数量
            UserTb userTb = userTbService.selectGameUserById(sysUser.getGameUserId().intValue());
            sysUser.setGameUser(userTb);
        }
        return AjaxResult.success("操作成功",sysUser);
    }

}
